package com.eascs.placeonfile.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.eascs.admin.o2o.internal.util.StatusContexts;
import com.eascs.admin.o2o.loan.entity.O2oLoanapplyEntity;
import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.file.FileTypeConstants;
import com.eascs.placeonfile.dao.O2oFileDao;
import com.eascs.placeonfile.entity.O2oFileEntity;
import com.eascs.placeonfile.entity.O2oFileFormEntity;

@Service
public class O2oFileService {

	@Autowired
	O2oFileDao fileDao;

	public PageResult<Object[]> findByPage(O2oFileFormEntity entity) {
		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, entity.getPageSize(), entity.getPageIndex());
		StringBuffer querySql = new StringBuffer();
		querySql.append("SELECT OPOF.* FROM O2O_PLACE_ON_FILE OPOF WHERE 1=1 ");

		sqlPageQuery.setQuerySql(querySql.toString());
		return fileDao.findDataWithPage(sqlPageQuery);
	}

	@Transactional(transactionManager = "o2oTransactionManager")
	public int insert(O2oFileEntity entity) {
		return fileDao.insert(entity);
	}

	@Transactional(transactionManager = "o2oTransactionManager")
	public int updateById(O2oFileEntity entity) {
		return fileDao.updateById(entity);
	}

	public O2oFileEntity getEntity(String loanapplyNo) {
		String sql = "SELECT OPOF.* FROM O2O_PLACE_ON_FILE OPOF WHERE OPOF.LOANAPPLY_NO=? ";
		return fileDao.findEntity(sql, O2oFileEntity.class, loanapplyNo);
	}
	
	public String getInputTime(String loanapplyNo){
		String sql = "SELECT OPOF.INPUT_DATE FROM O2O_PLACE_ON_FILE OPOF WHERE OPOF.LOANAPPLY_NO=?";
		return fileDao.findString(sql, loanapplyNo);
	}

	@Transactional(transactionManager = "o2oTransactionManager")
	public int updateIsSuccessFile(String loanapplyNo) {
		String sql = "UPDATE O2O_PLACE_ON_FILE OPOF SET OPOF.IS_FILE_SUCCESS=? WHERE OPOF.LOANAPPLY_NO=? ";
		return fileDao.execute(sql, StatusContexts.O2O_YES, loanapplyNo);
	}
	
	public List<O2oLoanapplyEntity> getLoans(){
		String sql = "SELECT OL.* FROM O2O_LOANAPPLY OL WHERE OL.LOAN_FLG=? AND OL.IS_PLACE_ON_FILE=?";
		return fileDao.findEntitys(sql, O2oLoanapplyEntity.class, StatusContexts.O2O_YES, StatusContexts.WFTYPE_STATUS_NEW);
	}
	
	public List<O2oLoanapplyEntity> getLoansArchive(Date StartDate){
		String sql = "SELECT OL.* FROM O2O_LOANAPPLY OL WHERE OL.LOAN_FLG=? AND OL.IS_PLACE_ON_FILE=? AND create_date > ?";
		return fileDao.findEntitys(sql, O2oLoanapplyEntity.class, StatusContexts.O2O_YES, StatusContexts.WFTYPE_STATUS_NEW,StartDate);
	}

	/**
	 * 查询Code 判断是否附件上传完整
	 */
	public boolean checkDataCode(String loanapplyNo) {
		boolean flag = true;
		String sql = "select d.datalist_code from o2o_datalist d where d.loanapply_no=?";
		List<String> code = fileDao.findSimpleObjectList(sql, String.class, loanapplyNo);
		if (code != null && code.size() > 0) {
			if (!code.contains(FileTypeConstants.DALIST_TYPE_FREIGHT)) {
				flag = false;
			}
		} else {
			flag = false;
		}
		return flag;
	}

}
